package models

import (
	"strconv"
	_ "time"

	orm "go-papers/database"
)

type MemberCollect struct {
	CollectId    int    `json:"collectId" gorm:"type:int(11) unsigned;primary_key"` //
	MemberId     int    `json:"memberId" gorm:"type:int(11);"`                      // 用户id
	ThirdUserId  int    `json:"thridUserId" gorm:"type:int(11);"`                   // 用户第三方账户 id
	CourseId     int    `json:"courseId" gorm:"type:int(11);"`                      // 课程id
	CourseName   string `json:"courseName" gorm:"type:varchar(255);"`               // 课程名称
	OrganFeature string `json:"OrganFeature" gorm:"-"`                              // 课程名称

	CourseType     string `json:"courseType"  gorm:"-"`     // 课程类目 课程类别  素质教育
	CourseCategory string `json:"courseCategory"  gorm:"-"` // 学科科目 语文/数学

	OrganId     int    `json:"organId"  gorm:"-"`     // 授课机构
	OrganName   string `json:"organName"  gorm:"-"`   // 机构名称
	CourseTeach string `json:"courseTeach"  gorm:"-"` // 授课方式 ：线上 、 线下

	CourseExpensive string `json:"courseExpensive"  gorm:"-"` // 是否 贵就赔 1 是 / 0 否

	CoursePrice   float64 `json:"coursePrice"  gorm:"-"`   // 授课金额
	DiscountPrice float64 `json:"discountPrice"  gorm:"-"` // 优惠金额

	CourseTime string `json:"courseTime"  gorm:"-"` // 授课时间 字符串
	CourseAge  string `json:"courseAge"  gorm:"-"`  // 授课适合年龄 1 :0～3岁 3 :3～6岁 6 :7-12岁 9:12-18岁  12: 18岁+

	CourseVideo    string `json:"courseVideo"  gorm:"-"`    // 介绍视频 json
	CourseImg      string `json:"courseImg"  gorm:"-"`      // 介绍图片 json
	CourseSystem   string `json:"courseSystem"  gorm:"-"`   // 课程体系
	CourseFeature  string `json:"courseFeature"  gorm:"-"`  // 课程特色
	CourseTools    string `json:"courseTools"  gorm:"-"`    // 课程教具
	CourseService  string `json:"courseService"  gorm:"-"`  // 课程服务体系
	CourseTransfer string `json:"courseTransfer"  gorm:"-"` // 支付转让
	CourseFree     string `json:"courseFree"  gorm:"-"`     // 支付冻结
	CourseRefund   string `json:"courseRefund"  gorm:"-"`   // 退费规则
	CourseLeave    string `json:"courseLeave" gorm:"-"`     // 请假制度

	CourseInfo string `json:"courseInfo"  gorm:"-"` // 介绍详情

	CourseStatus string `json:"courseStatus"  gorm:"-"` // 核准状态 1 待审核 ，3 失败 ， 5 已审核

	DataScope string `json:"dataScope" gorm:"-"`
	Params    string `json:"params"  gorm:"-"`
	OpenId    string `json:"openId"  gorm:"-"`
	Token     string `json:"token"  gorm:"-"`
	BaseModel
}

type MemberCollectNew struct {
	CollectId    int    `json:"collectId" gorm:"type:int(11) unsigned;primary_key"` //
	MemberId     int    `json:"memberId" gorm:"type:int(11);"`                      // 用户id
	ThirdUserId  int    `json:"thridUserId" gorm:"type:int(11);"`                   // 用户第三方账户 id
	CourseId     int    `json:"courseId" gorm:"type:int(11);"`                      // 课程id
	CourseName   string `json:"courseName" gorm:"type:varchar(255);"`               // 课程名称
	OrganFeature string `json:"organFeature" gorm:"type:varchar(255);"`             // 课程名称

	CourseType     string `json:"courseType"  gorm:"-"`     // 课程类目 课程类别  素质教育
	CourseCategory string `json:"courseCategory"  gorm:"-"` // 学科科目 语文/数学

	OrganId     int    `json:"organId"  gorm:"-"`     // 授课机构
	OrganName   string `json:"organName"  gorm:"-"`   // 机构名称
	CourseTeach string `json:"courseTeach"  gorm:"-"` // 授课方式 ：线上 、 线下

	CourseExpensive string `json:"courseExpensive"  gorm:"-"` // 是否 贵就赔 1 是 / 0 否

	CoursePrice   float64 `json:"coursePrice"  gorm:"-"`   // 授课金额
	DiscountPrice float64 `json:"discountPrice"  gorm:"-"` // 优惠金额

	CourseTime string `json:"courseTime"  gorm:"-"` // 授课时间 字符串
	CourseAge  string `json:"courseAge"  gorm:"-"`  // 授课适合年龄 1 :0～3岁 3 :3～6岁 6 :7-12岁 9:12-18岁  12: 18岁+

	CourseVideo    string `json:"courseVideo"  gorm:"-"`    // 介绍视频 json
	CourseImg      string `json:"courseImg"  gorm:"-"`      // 介绍图片 json
	CourseSystem   string `json:"courseSystem"  gorm:"-"`   // 课程体系
	CourseFeature  string `json:"courseFeature"  gorm:"-"`  // 课程特色
	CourseTools    string `json:"courseTools"  gorm:"-"`    // 课程教具
	CourseService  string `json:"courseService"  gorm:"-"`  // 课程服务体系
	CourseTransfer string `json:"courseTransfer"  gorm:"-"` // 支付转让
	CourseFree     string `json:"courseFree"  gorm:"-"`     // 支付冻结
	CourseRefund   string `json:"courseRefund"  gorm:"-"`   // 退费规则
	CourseLeave    string `json:"courseLeave" gorm:"-"`     // 请假制度

	CourseInfo   string `json:"courseInfo"  gorm:"-"`   // 介绍详情
	CourseStatus string `json:"courseStatus"  gorm:"-"` // 核准状态 1 待审核 ，3 失败 ， 5 已审核
}

func (MemberCollect) TableName() string {
	return "member_collect"
}

// 创建MemberCollect
func (e *MemberCollect) Create() (MemberCollect, error) {
	var doc MemberCollect
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

func (e *MemberCollect) GetV2() (MemberCollect, error) {
	var doc MemberCollect
	table := orm.Eloquent.Table(e.TableName())
	if e.CollectId != 0 {
		table = table.Where("collect_id = ?", e.CollectId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	} else if e.ThirdUserId != 0 {
		table = table.Where("third_user_id = ?", e.ThirdUserId)
	}
	if e.CourseId != 0 {
		table = table.Where("course_id = ?", e.CourseId)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取MemberCollect
func (e *MemberCollect) Get() (MemberCollect, error) {
	var doc MemberCollect
	table := orm.Eloquent.Table(e.TableName())
	if e.CollectId != 0 {
		table = table.Where("collect_id = ?", e.CollectId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.CourseId != 0 {
		table = table.Where("course_id = ?", e.CourseId)
	}
	if e.ThirdUserId != 0 {
		table = table.Where("third_user_id = ?", e.ThirdUserId)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取MemberCollect带分页
func (e *MemberCollect) GetPage(pageSize int, pageIndex int) ([]MemberCollectNew, int, error) {
	var doc []MemberCollectNew

	table := orm.Eloquent.Select("*, course.*,organ.organ_feature").Table(e.TableName())
	table = table.Joins("inner join course on course.course_id = member_collect.course_id ")
	table = table.Joins("inner join organ on  `organ`.organ_id = course.organ_id ")
	if e.CollectId != 0 {
		table = table.Where("`member_collect`.collect_id = ?", e.CollectId)
	}
	if e.MemberId != 0 {
		table = table.Where("`member_collect`.member_id = ?", e.MemberId)
	} else if e.ThirdUserId != 0 {
		table = table.Where("`member_collect`.third_user_id = ?", e.ThirdUserId)
	}
	if e.CourseId != 0 {
		table = table.Where("`member_collect`.course_id = ?", e.CourseId)
	}
	if e.CourseName != "" {
		table = table.Where("`course`.course_name like ?", "%"+e.CourseName+"%")
	}

	var count int

	if err := table.Offset((pageIndex-1)*pageSize).Order(" `member_collect`.collect_id  desc", true).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`member_collect`.`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新MemberCollect
func (e *MemberCollect) Update(id int) (update MemberCollect, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("collect_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除MemberCollect
func (e *MemberCollect) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("collect_id = ?", id).Delete(&MemberCollect{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *MemberCollect) BatchDelete(id []int) (Result bool, err error) {

	table := orm.Eloquent.Table(e.TableName())
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.ThirdUserId != 0 {
		table = table.Where("third_user_id = ?", e.ThirdUserId)
	}
	if err = table.Where("collect_id in (?)", id).Delete(&MemberCollect{}).Error; err != nil {
		return
	}
	Result = true
	return
}

func (e *MemberCollect) BatchDeleteV2(ids string, courseIds string) (Result bool, err error) {

	table := orm.Eloquent.Table(e.TableName())
	sql := " DELETE FROM  member_collect  WHERE   "
	if ids != "" {
		sql = sql + " collect_id in (" + ids + ") "
	} else if courseIds != "" {
		sql = sql + " course_id  in (" + courseIds + ") "
	}

	if e.MemberId != 0 {
		sql = sql + " AND member_id = " + strconv.Itoa(e.MemberId) + " "
	} else if e.ThirdUserId != 0 {
		sql = sql + " AND third_user_id = " + strconv.Itoa(e.ThirdUserId) + " "
	}
	if err = table.Exec(sql).Error; err != nil {
		return
	}
	Result = true
	return
}
